package com.cat.dynamicPlanning;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/minimum-operations-to-make-the-array-k-increasing/
 * @create 2025/9/4 14:06
 * @since JDK17
 */

public class Solution67 {
    int f(int[] ends, int n, int target) {
        if (n == 0 || ends[n - 1] < target) {
            return n;
        }
        int l = -1, r = n, mid;
        while (l + 1 < r) {
            mid = (l + r) / 2;
            if (ends[mid] > target) {
                r = mid;
            } else {
                l = mid;
            }
        }
        return r;
    }
    public int lengthOfLIS(int[] nums, int n) {
        int cnt = 0;
        int[] ends = new int[n];
        for (int i = 0; i < n; i++) {
            int j = f(ends, cnt, nums[i]);
            if (j == cnt) {
                ends[cnt++] = nums[i];
            } else {
                ends[j] = nums[i];
            }
        }

        return cnt;
    }
    public int kIncreasing(int[] arr, int k) {
        int n = arr.length, ans = 0;
        int[] nums = new int[n];
        for (int i = 0; i < k; i++) {
            int cnt = 0;
            for (int j = i; j < n; j += k) {
                nums[cnt++] = arr[j];
            }
            ans += cnt - lengthOfLIS(nums, cnt);
        }
        return ans;
    }
}
